package com.harmontronics.hanma.paas.paas.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.harmontronics.hanma.paas.paas.annotation.Log;
import com.harmontronics.hanma.paas.paas.common.PageBase;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.domain.app.entity.HmModel;
import com.harmontronics.hanma.paas.paas.domain.app.entity.valueobject.ModelTypeEnum;
import com.harmontronics.hanma.paas.paas.domain.app.service.dialect.ViewColumn;
import com.harmontronics.hanma.paas.paas.dto.input.model.*;
import com.harmontronics.hanma.paas.paas.dto.output.HmModelResult;
import com.harmontronics.hanma.paas.paas.service.IHmModelAppService;
import com.harmontronics.hanma.paas.paas.utils.ParamUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * ${comments}
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-03-31 16:40:00
 */
@RestController
@RequestMapping("/api/hmmodel")
@Api(tags = {"模型"})
public class HmModelController {
    @Autowired
    private IHmModelAppService hmModelappService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    public ResponseEntity<List<HmModelResult>> list() {
        List<HmModel> hmModelList = hmModelappService.getList();
        return ResponseEntity.ok(Convert.toList(HmModelResult.class, hmModelList));
    }

    /**
     * 分页列表
     */
    @GetMapping("/page/list")
    @ApiOperation("分页列表")
    public ResponseEntity<PageBase<HmModelResult>> list(
            @ApiParam(value = "类型") @RequestParam(required = false) String modelTypes,
            @ApiParam(value = "模型名称") @RequestParam(required = false) String name,
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
            @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize) {
        PageBase<HmModel> hmModelPageBase = hmModelappService.getList(modelTypes, name, pageNo, pageSize);
        PageBase<HmModelResult> resultPageBase = PageBase.copy(hmModelPageBase, HmModelResult.class);
        return ResponseEntity.ok(resultPageBase);
    }


    /**
     * 信息
     */
    @GetMapping("/info")
    @ApiOperation("详情")
    public ResponseEntity<HmModelResult> info(@RequestParam("id") Long id) {
        HmModel hmModel = hmModelappService.findById(id);
        HmModelResult result = BeanUtil.toBean(hmModel, HmModelResult.class);
        return ResponseEntity.ok(result);
    }

    /**
     * 保存
     */
    @PostMapping()
    @ApiOperation("保存")
    @Log("新增了模型")
    public ResponseEntity<Long> save(@Valid @RequestBody HmModelCreateInput hmModelInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmModel hmModel = BeanUtil.toBean(hmModelInput, HmModel.class);
        hmModel.setSupportProcess(hmModelInput.getSupportProcess()==null?false:hmModelInput.getSupportProcess());
        hmModel.setSupportTree(hmModelInput.getSupportTree()==null?false:hmModelInput.getSupportTree());
        Long id = hmModelappService.save(hmModel);
        return ResponseEntity.ok(id);
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    @Log("修改了模型")
    public ResponseEntity<String> update(@Valid @RequestBody HmModelUpdateInput hmModelInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmModel hmModel = BeanUtil.toBean(hmModelInput, HmModel.class);
        hmModelappService.update(hmModel);
        return ResponseEntity.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping()
    @ApiOperation("删除")
    @Log("删除了模型")
    public ResponseEntity<String> delete(@RequestBody Long[] ids) {
        hmModelappService.remove(Arrays.asList(ids));
        return ResponseEntity.ok();
    }

    @GetMapping("valid-field")
    @ApiOperation("字段重复检查")
    public ResponseEntity<String> validField(@RequestParam String field, @RequestParam String value, @RequestParam(required = false) Long id) {
        hmModelappService.validField(id, field, value);
        return ResponseEntity.ok();
    }


    @PostMapping("data/add")
    @ApiOperation("新增模型表数据")
    // @Log("新增了模型数据")
    @Deprecated
    public ResponseEntity<String> addData(@Valid @RequestBody HmModelDataInput hmModelDataInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        hmModelappService.addData(hmModelDataInput.getPageId(), hmModelDataInput.getData());
        return ResponseEntity.ok();
    }

    @GetMapping("data/get")
    @ApiOperation("获取模型表数据")
    @Deprecated
    public ResponseEntity<Map<String, Object>> getData(@RequestParam Long pageId, @RequestParam Long id) {
        Map<String, Object> data = hmModelappService.getData(pageId, id);
        return ResponseEntity.ok(data);
    }

    @PutMapping("data/edit")
    @ApiOperation("修改模型表数据")
    // @Log("修改了模型数据")
    @Deprecated
    public ResponseEntity<String> editData(@Valid @RequestBody HmModelDataInput hmModelDataInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        hmModelappService.editData(hmModelDataInput.getPageId(), hmModelDataInput.getData());
        return ResponseEntity.ok();
    }

    @DeleteMapping("data/delete")
    @ApiOperation("删除模型表数据")
    // @Log("删除了模型数据")
    @Deprecated
    public ResponseEntity<String> deleteData(@Valid @RequestBody HmModelDataDeleteInput hmModelDataInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        hmModelappService.deleteData(hmModelDataInput.getPageId(), hmModelDataInput.getDataId());
        return ResponseEntity.ok();
    }

    @PostMapping("data/list")
    @ApiOperation("模型数据列表")
    @Deprecated
    public ResponseEntity<PageBase<Map<String, Object>>> dataList(@Valid @RequestBody HmModelDataQueryInput queryInput, BindingResult bindingResult) throws SQLException {
        ParamUtils.valid(bindingResult);
        return ResponseEntity.ok(hmModelappService.listData(queryInput.getPageId(), queryInput.getParams(), queryInput.getOrderRules(), queryInput.getPageNo(), queryInput.getPageSize()));
    }

    @GetMapping("view-column")
    @ApiOperation("获取视图列信息")
    public ResponseEntity<List<ViewColumn>> getViewColumns(@RequestParam Long modelId) {
        List<ViewColumn> viewColumns = hmModelappService.getViewColumns(modelId);
        return ResponseEntity.ok(viewColumns);
    }
}
